//
//  LFTextField.m
//  Logistics
//
//  Created by 刘峰 on 2017/10/20.
//  Copyright © 2017年 com.Logistics. All rights reserved.
//

#import "LFTextField.h"

@implementation LFTextField

@synthesize delegate = _delegate;

-(void)setLimitNum:(NSInteger)limitNum {
    _limitNum = limitNum;
    if (limitNum > 0) {
        [self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (_limitNum > 0) {
        
        UITextRange *selectRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectRange.start offset:0];
        if (!position && textField.text.length > _limitNum) {
            self.text = [textField.text substringToIndex:_limitNum];
        }
        
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(textField:textDidChange:)]) {
        [self.delegate textField:textField textDidChange:textField.text];
    }
}





- (void)drawPlaceholderInRect:(CGRect)rect
{
    if (self.placeHolderFont ||self.placeholderColor ) {
        // 创建一个富文本对象
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        // 设置富文本对象的颜色
        attributes[NSForegroundColorAttributeName] = _placeholderColor;
        attributes[NSFontAttributeName] = self.placeHolderFont;
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        attributes[NSParagraphStyleAttributeName] = style;
        [self.placeholder drawInRect:CGRectMake(rect.origin.x, 20, rect.size.width - 40, self.placeHolderFont.lineHeight) withAttributes:attributes
         ];
    }else {
        [super drawPlaceholderInRect:rect];
    }
 

}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
